package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.Material;
import com.frcdi.qms.masterdata.service.IMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料Controller
 *
 * @author cell
 * @date 2023-09-11
 */
@Api(tags = "物料")
@RestController
@RequestMapping("/masterData/material")
public class MaterialController extends BaseController
{
    @Autowired
    private IMaterialService materialService;

    /**
     * 查询物料列表
     */
    @ApiOperation("查询物料列表")
    @PreAuthorize("@ss.hasPermi('masterData:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(Material material)
    {
        startPage();
        List<Material> list = materialService.selectMaterialList(material);
        return getDataTable(list);
    }

    /**
     * 导出物料列表
     */
    @ApiOperation("导出物料列表")
    @PreAuthorize("@ss.hasPermi('masterData:material:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Material material)
    {
        List<Material> list = materialService.selectMaterialList(material);
        ExcelUtil<Material> util = new ExcelUtil<Material>(Material.class);
        util.exportExcel(response, list, "物料数据");
    }

    /**
     * 获取物料详细信息
     */
    @ApiOperation("获取物料详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:material:query')or @ss.hasPermi('workflow:flowable:list')or @ss.hasPermi('masterData:execution:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialService.selectMaterialById(id));
    }

    /**
     * 新增物料
     */
    @ApiOperation("新增物料")
    @PreAuthorize("@ss.hasPermi('masterData:material:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Material material)
    {
        if (!materialService.checkmaterialNameUnique(material))
        {
            return error("新增物料'" + material.getMaterialName() + "'失败，名称已存在");
        }
        material.setCreateBy(getUsername());
        return success(materialService.insertMaterial(material));
    }

    /**
     * 修改物料
     */
    @ApiOperation("修改物料")
    @PreAuthorize("@ss.hasPermi('masterData:material:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Material material)
    {
        if (!materialService.checkmaterialNameUnique(material))
        {
            return error("修改物料'" + material.getMaterialName() + "'失败，名称已存在");
        }
        material.setUpdateBy(getUsername());
        // 编辑时重置所有状态
        material.setStatus(UserConstants.DISABLE);
        material.setAuditResult(UserConstants.AUDIT_NOT_AUDITED);
        material.setSend(UserConstants.SEND_NOT_RETURNED);
        material.setTaskStatus(UserConstants.TASK_NOT);
        return toAjax(materialService.updateMaterial(material));
    }

    /**
     * 修改流程编号
     */
    @ApiOperation("修改流程编号")
    @PreAuthorize("@ss.hasPermi('masterData:material:edit')")
    @PutMapping("/material")
    public AjaxResult editById(@RequestBody Material material)
    {
        material.setUpdateBy(getUsername());
        return toAjax(materialService.updateMaterial(material));
    }

    /**
     * 删除物料
     */
    @ApiOperation("删除物料")
    @PreAuthorize("@ss.hasPermi('masterData:material:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids)
    {
        return toAjax(materialService.deleteMaterialByIds(ids));
    }

    /**
     * 提交至申请人
     */
    @ApiOperation("提交至申请人")
    @PreAuthorize("@ss.hasPermi('masterData:material:add')or @ss.hasPermi('masterData:material:edit')")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Material material)
    {
        return toAjax(materialService.submitApplicant(material));
    }

    @ApiOperation("获取编码")
    @PreAuthorize("@ss.hasPermi('masterData:material:add')")
    @GetMapping("/getCode")
    public AjaxResult getcode(){
        return success(materialService.getCode());
    }
}
